package com.qms.tubu.fragment.account;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.qms.tubu.R;
import com.qms.tubu.activity.account.RegisterActivity;
import com.qms.tubu.bean.AccountBean;
import com.qms.tubu.dialog.TimeDialog;
import com.qms.tubu.fragment.BaseFragment;

import java.util.Calendar;
import java.util.regex.Pattern;

import tool.lib.entity.BirthBean;

/**
 * Created by Mad on 2015/7/22.
 */
public class CreatePersonalFragment extends BaseFragment implements View.OnClickListener,TextWatcher,TimeDialog.TimeSelectedListener{
    private EditText mNameEdit;
    private TextView mSexText,mPositionText,mNextStepText,mBirthdayText;
    private FrameLayout mNextStepBtn;
    private int mSex;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView=inflater.inflate(R.layout.fragment_create_personal,container,false);
        mNameEdit= (EditText) rootView.findViewById(R.id.create_personal_name_edit);
        mSexText= (TextView) rootView.findViewById(R.id.create_personal_sex_text);
        mPositionText= (TextView) rootView.findViewById(R.id.create_personal_location_text);
        mNextStepBtn= (FrameLayout) rootView.findViewById(R.id.create_personal_next_step_btn);
        mNextStepText= (TextView) rootView.findViewById(R.id.create_personal_next_step_text);
        mBirthdayText= (TextView) rootView.findViewById(R.id.create_personal_birthday_text);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mNameEdit.addTextChangedListener(this);
        mSexText.setOnClickListener(this);
        mBirthdayText.setOnClickListener(this);
        mNextStepBtn.setOnClickListener(this);
        setNextStepEnabled(false);
        mSex=1;
    }
    private void setNextStepEnabled(boolean enabled)
    {
        mNextStepBtn.setEnabled(enabled);
        mNextStepText.setEnabled(enabled);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.create_personal_sex_text:
                showSelectSex();
                break;
            case R.id.create_personal_birthday_text:
                TimeDialog dialog=new TimeDialog(rootView.getContext(),mBirthdayText.getText().toString());
                dialog.setMaxDate(System.currentTimeMillis());
                dialog.setOnTimeSelectedListener(this);
                dialog.show();
                break;
            case R.id.create_personal_next_step_btn:
                AccountBean account=new AccountBean();
                account.setNickname(mNameEdit.getText().toString());
                account.setBirthday(mBirthdayText.getText().toString());
                account.setSex(mSex);
                account.setProvince("浙江");
                account.setCity("温州");
                RegisterActivity activity= (RegisterActivity) getActivity();
                activity.getSupportActionBar().setTitle("设置账号与密码");
                FragmentTransaction transaction=activity.getSupportFragmentManager().beginTransaction();
                RegisterFragment fragment=new RegisterFragment();
                Bundle bundle=new Bundle();
                bundle.putSerializable("account",account);
                fragment.setArguments(bundle);
                transaction.replace(R.id.register_fragment_container, fragment, "register");
                transaction.commit();
                break;
        }
    }

    @Override
    public boolean onSelected(BirthBean birthday, String birthdayStr) {
        mBirthdayText.setText(birthdayStr);
        return true;
    }

    private void showSelectSex()
    {
        AlertDialog.Builder builder=new AlertDialog.Builder(rootView.getContext());
        builder.setTitle("性别");
        builder.setSingleChoiceItems(R.array.sex_array, mSex - 1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                if(mSex==which+1)
                    return;
                mSex=which+1;
                String sexStr=which==0?"男":"女";
                mSexText.setText(sexStr);
                dialog.dismiss();
            }
        });
        builder.show();
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        Pattern pattern= Pattern.compile("^[a-zA-Z0-9\\u4e00-\\u9fa5_-]{3,16}$");
        setNextStepEnabled(pattern.matcher(s.toString()).find());
    }
}
